import unittest

from parameterized import parameterized

from config import BASE_DIR
from api.Userlist import UserTest
from common.read_json import read_json_data


# 查询用户列表测试类
class Test_UserList(unittest.TestCase):

    def setUp(self) -> None:
        print("\n")
        print("=" * 110)
        print("\n")

    # 无查询条件查询成功
    def test_case_01(self):
        data1 = {"page_no": 1, "page_size": 10}
        resp = UserTest.get_list(data1)
        print(resp.json())
        self.assertEqual(resp.status_code, 200)

    # 查询杨铭铭
    def test_case_02(self):
        data1 = {"page_no": 1, "page_size": 10, "nickname": "杨铭铭"}
        resp = UserTest.get_list(data1)
        print(resp.json())
        self.assertEqual(resp.status_code, 200)

    path_filename = BASE_DIR + "/data/data_member.json"

    # 查看用户的详情：参数化
    @parameterized.expand(read_json_data(path_filename))
    def test_case_03(self, duck, memberId):
        member_id = memberId
        resp = UserTest.details(member_id)
        print(duck, ":", resp.json())
        self.assertEqual(resp.status_code, 200)

# # 修改用户信息测试类
# class Test_User_revise(unittest.api):
#     def setUp(self) -> None:
#         print("\n")
#         print("=" * 110)
#         print("\n")
#
#     # 修改姓名
#     def test_case_01(self):
#         body = {"member_id": "1641355789378039809", "nickname": "杨铭铭"}
#         resp = UserTest.put_user(body)
#         print(resp.json())



#  优化代码
# import unittest
# from parameterized import parameterized
# from config import BASE_DIR
# from api.Userlist import UserTest
# from common.read_json import read_json_data
#
# class TestUserList(unittest.TestCase):
#
#     def setUp(self) -> None:
#         self.base_url = BASE_DIR + "/data/data_member.json"
#         self.default_data = {"page_no": 1, "page_size": 10}
#
#     def print_response(self, response, description=""):
#         print(f"{description}: {response.json()}")
#
#     def test_query_without_condition_success(self):
#         resp = UserTest.get_list(self.default_data)
#         self.print_response(resp, "Query without condition")
#         self.assertEqual(resp.status_code, 200)
#
#     def test_query_specific_user_success(self):
#         data = self.default_data.copy()
#         data["nickname"] = "杨铭铭"
#         resp = UserTest.get_list(data)
#         self.print_response(resp, "Query specific user")
#         self.assertEqual(resp.status_code, 200)
#
#     @parameterized.expand(read_json_data(self.base_url))
#     def test_view_user_details(self, duck, memberId):
#         resp = UserTest.details(memberId)
#         self.print_response(resp, f"View details of {duck}")
#         self.assertEqual(resp.status_code, 200)
#
# if __name__ == '__main__':
#     unittest.main()